/*
 * SomeStatusForm.java
 *
 * Created on 22 Январь 2010 г., 12:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package chatty.forms;

import chatty.Chatty;
import chatty.cl.ContactList;
import chatty.ui.menu.*;
import chatty.modules.*;

import protocol.Protocol;
import protocol.Status;
import protocol.StatusInfo;

/**
 *
 * @author Vladimir Krukov
 */
public class SomeStatusForm implements SelectListener {
    
	public static final int SF_STATUS_LIST = 1;
	public static final int SF_STATUS_LIST2 = 2;
	
    private MenuModel menu = new MenuModel(MenuModel.MENU_CENTER);
	//private InputTextBox message = new InputTextBox("status_message", 256);
    private Object prevDisplay;
	private int type = SF_STATUS_LIST;
    protected Protocol protocol;
    protected int statusIndex;
	
    public SomeStatusForm(Protocol protocol, int type) {
        this.protocol = protocol;
		this.type = type;
    }

    // You can overload it
    protected void addStatuses(MenuModel menu) {
        addStatuses(menu, protocol.getStatusList());
		
		if (type != SF_STATUS_LIST)
			menu.addItem(	protocol.getStatusInfo().getName(Status.I_STATUS_OFFLINE), 
							protocol.getStatusInfo().getIcon(Status.I_STATUS_OFFLINE),
							Status.I_STATUS_OFFLINE
						);
    }
    // You can overload it
    protected void statusSelected(int statusIndex) {
        setStatus(statusIndex, null);
        back();
    }

    private void prepare() {
        menu.clean();
        addStatuses(menu);
        menu.setActionListener(this);
    }
    protected final void setStatus(int statusIndex, String str) {
        protocol.setOnlineStatus(statusIndex, str);
    }
    private final void addStatuses(MenuModel menu, byte[] statuses) {
        StatusInfo info = protocol.getStatusInfo();
        for (int i = 0; i < statuses.length; ++i) {
            menu.addItem(info.getName(statuses[i]), info.getIcon(statuses[i]), statuses[i]);
        }
        menu.setDefaultItemCode(protocol.getProfile().statusIndex);
    }

    public final void select(Select select, MenuModel model, int statusIndex) {
        
		if ( type != SF_STATUS_LIST ) {
			if ( statusIndex != Status.I_STATUS_OFFLINE ) {
				this.statusIndex = statusIndex;
				statusSelected(statusIndex);
				if ( !protocol.isConnected() ) protocol.connect();
			} else {
				protocol.disconnect();
				back();
			}
		} else {
			this.statusIndex = statusIndex;
			statusSelected(statusIndex);
		}
    }
    public final void show() {
        prepare();
        prevDisplay = Chatty.getCurrentDisplay();
        new Select(menu).show();
    }
    public final void back() {
        ContactList.updateMainMenu();
        Chatty.setDisplay(prevDisplay);
    }
}
